/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject.ui.graphical.tools.base;

import java.awt.Image;
import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.imageio.ImageIO;

import org.ceroproject.games.*;
import org.ceroproject.ui.graphical.tools.Deck;
import org.ceroproject.ui.graphical.tools.GraphicsCard;



/**
 * Class creating a Graphical Card associated to a Card
 * 
 * @author Matt
 */
public class GraphicsCardBase extends RectangleBase implements GraphicsCard {

	/* -------------- field ------------- */

	/** Card associated to GraphicalCard */
	private Card card;

	/** The image path of the card */
	private String path;
	
	/** The image name of the card */
	private String name;
	
	/** Image's scale */
	private double scale;
	
	/** Color of the selected Card */
	private Color colorCardSelected;
	
	/** Selected Cards */
	private static List<GraphicsCard> selectedCards = new ArrayList<GraphicsCard>();
	
	/** Mapping the Card Name and its Image */
	private static HashMap<String,Image> imageMap = new HashMap<String, Image>();
	

	/* ----------- Constructor ---------- */

	/**
	 * Build a GraphicalCard associated to a Card
	 * 
	 * @param card
	 *            Card associated to the Graphical Card
	 */
	public GraphicsCardBase(Card card) {
		
		// initialize
		this.card = card;
		scale = 1.5;
		path = "Texture/";
		name = card.getCardName();
		colorCardSelected = new Color(0,155,255);
	}
	
	
	/* ------------- Method ------------- */

	/**
	 * Returns the Card
	 * 
	 * @return the Card
	 */
	public Card getCard() {
		return card;
	}
	
	/**
	 * Set the image path of the card
	 * 
	 * @param path the image path of the card
	 */
	public void setImgPath(String path){
		this.path = path;
	}
	
	
	/**
	 * Return the image path of the card
	 * 
	 * @return the image path of the card
	 */
	public String getImgPath(){
		return path;
	}
	
	
	/**
	 *	If the ImageMap does not contain the card name,
	 *	this method puts the Card Name and its Image,
	 *  nothing otherwise
	 * 
	 * @param imgName
	 * 			the card name
	 */
	private void updateMappingImg(String imgName){
		if (!imageMap.containsKey(path + imgName)){
			Image img;
			try {
				img = ImageIO.read(new File(path + imgName + ".png"));
				imageMap.put(path + imgName,img);
			} catch (IOException e) {
				System.err.println(e);
			}
			
		}
	}
	
	
	/**
	 * Return the image's scale
	 * 
	 * @return the image's scale
	 */
	public double getScale() {
		return scale;
	}
	
	
	/**
	 * Return the status of the Graphical Card
	 * 
	 * @return true if the graphical card has been selected, false otherwise
	 */
	public boolean getSelected(){
		 return selectedCards.contains(this);
	}

	
	/**
	 * Select the graphical card
	 * 
	 * @param state
	 *            true if the card has been selected, false otherwise
	 */
	public void setSelected(boolean state){
		if (state)
			  selectedCards.add(this);
			else
			  selectedCards.remove(this);
	}
	
	
	/**
	 * Return the coordinates of the GraphicsCard
	 * 
	 * @return the coordinates of the Rectangle
	 */
	public String toString(){
		return "name : " + getCard().getCardName() + super.toString();
	}

	
	/**
	 * Set the Color of the selected card
	 * 
	 * @param colorCardSelected
	 * 					the Color of the selected card
	 */
	public void setColorCardSelected(Color colorCardSelected){
		this.colorCardSelected = colorCardSelected;
	}
	
	
	/**
	 *	Paint the selected card
	 *	
	 *	@param g
	 *            Graphics to paint
	 */
	protected void paintCardSelected(Graphics g){
		g.setColor(colorCardSelected);
		g.fillRect(this.getX() - this.getWidth()/14, this.getY() - this.getWidth()/14,
				this.getWidth() + this.getWidth()/7, this.getHeight() + this.getHeight()/10);
	}
	
	
	/**
	 * Paint the Graphical Card
	 * 
	 * @param g
	 *            Graphics to paint
	 * @param deck 
	 *				the deck where to paint the Graphical zone
	 */
	public void paint(Graphics g, Deck deck){
		// update mapping image
		updateMappingImg(name);
		
		if (this.getSelected())
			paintCardSelected(g);
		
		// paint the graphical card
		g.drawImage(imageMap.get(path + name), this.getX(), this.getY(), this.getWidth(), this
				.getHeight(), deck);
	}
}
